//
//  ChangePassWordViewController.m
//  OptimumCarCharge
//
//  Created by 童巍 on 16/12/8.
//  Copyright © 2016年 童巍. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "Tool.h"
#import "SVProgressHUD.h"
#import "Masonry.h"
#import "WTMNetworkTool.h"

#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

@interface ChangePassWordViewController () <UITextFieldDelegate>
//@property (weak, nonatomic) IBOutlet UIView *viewUserPN;
@property (weak, nonatomic) IBOutlet UIView *viewOldPW;

@property (weak, nonatomic) IBOutlet UIView *viewNewPW;
@property (weak, nonatomic) IBOutlet UIView *viewSNPW;
@property (weak, nonatomic) IBOutlet UIButton *showPWButton;
@property (weak, nonatomic) IBOutlet UIButton *changeSureButton;
//@property (weak, nonatomic) IBOutlet UIImageView *viewUserImage;
//@property (weak, nonatomic) IBOutlet UITextField *viewUserTF;
@property (weak, nonatomic) IBOutlet UIImageView *viewOldImage;
@property (weak, nonatomic) IBOutlet UITextField *viewOldTF;
@property (weak, nonatomic) IBOutlet UITextField *viewNewTF;
@property (weak, nonatomic) IBOutlet UIImageView *viewNewImage;
@property (weak, nonatomic) IBOutlet UIImageView *viewSureImage;
@property (weak, nonatomic) IBOutlet UITextField *viewSureTF;
@property(nonatomic)BOOL showPW;
@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _showPW = NO;
    [self.view removeConstraints:[self.view constraints]];
   
    [self.viewOldTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [self.viewNewTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [self.viewSureTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.title = @"修改密码";
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     self.navigationController.navigationBarHidden = NO;
}

- (void)initUI
{
//   [self.viewUserPN mas_makeConstraints:^(MASConstraintMaker *make) {
//       make.width.mas_equalTo(kWidthOfPt(780));
//       make.height.mas_equalTo(kHeightOfPt(110));
//       make.centerX.mas_equalTo(self.view.mas_centerX);
//       make.top.mas_equalTo(kHeightOfPt(500));
//   }];
//    self.viewUserPN.layer.cornerRadius = kWidthOfPt(55);
//    self.viewUserPN.clipsToBounds = YES;
    
    [self.viewOldPW mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kHeightOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kHeightOfPt(500));
    }];
    self.viewOldPW.layer.cornerRadius = kWidthOfPt(60);
    self.viewOldPW.clipsToBounds = YES;

    [self.viewNewPW mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kHeightOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kHeightOfPt(500  + 170));
    }];
    self.viewNewPW.layer.cornerRadius = kWidthOfPt(60);
    self.viewNewPW.clipsToBounds = YES;

    
    [self.viewSNPW mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kHeightOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kHeightOfPt(500 + 170 + 170));
    }];
    self.viewSNPW.layer.cornerRadius = kWidthOfPt(60);
    self.viewSNPW.clipsToBounds = YES;


    [self.showPWButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.viewSNPW.mas_left).offset(kWidthOfPt(20));
        make.top.mas_equalTo(self.viewSNPW.mas_bottom).offset(kHeightOfPt(20));
        make.width.mas_equalTo(kWidthOfPt(280));
        make.height.mas_equalTo(kHeightOfPt(40));
    }];
    self.showPWButton.titleLabel.font = systemFontOfPX(40);
    
    [self.changeSureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kWidthOfPt(820));
        make.height.mas_equalTo(kHeightOfPt(120));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(kHeightOfPt(500 + 170 + 170 + 310));
    }];
    self.changeSureButton.layer.cornerRadius = kWidthOfPt(60);
    self.changeSureButton.titleLabel.font = systemFontOfPX(56);
    
   /** imageLayout ***/
//    [self.viewUserImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(kWidthOfPt(50));
//        make.centerY.mas_equalTo(self.viewUserPN.mas_centerY);
//        make.width.mas_equalTo(kWidthOfPt(60));
//        make.height.mas_equalTo(kHeightOfPt(60));
//    }];
    
    [self.viewOldImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewOldPW.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kHeightOfPt(60));
    }];
    
    [self.viewNewImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewNewPW.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kHeightOfPt(60));
    }];
    
    [self.viewSureImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWidthOfPt(50));
        make.centerY.mas_equalTo(self.viewSNPW.mas_centerY);
        make.width.mas_equalTo(kWidthOfPt(60));
        make.height.mas_equalTo(kHeightOfPt(60));
    }];
    
    /*** textfieldlayout ***/
//    [self.viewUserTF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.viewUserPN.mas_left).offset(kWidthOfPt(140));
//        make.right.mas_equalTo(self.viewUserPN.mas_right).offset( - kWidthOfPt(80));
//        make.centerY.mas_equalTo(self.viewUserPN.mas_centerY);
//        make.height.mas_equalTo(self.viewUserPN.mas_height);
//    }];
//    NSAttributedString *userPlaceHolder = [[NSAttributedString alloc]initWithString:@"请输入电话号码" attributes:@{NSFontAttributeName :systemFontOfPX(44),NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#f4f4f4"]}];
//    self.viewUserTF.attributedPlaceholder = userPlaceHolder;
    
    
    [self.viewOldTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.viewOldPW.mas_left).offset(kWidthOfPt(140));
        make.right.mas_equalTo(self.viewOldPW.mas_right).offset( - kWidthOfPt(80));
        make.centerY.mas_equalTo(self.viewOldPW.mas_centerY);
        make.height.mas_equalTo(self.viewOldPW.mas_height);
    }];
    
    NSAttributedString *oldPlaceHolder = [[NSAttributedString alloc]initWithString:@"请输入旧密码" attributes:@{NSFontAttributeName :systemFontOfPX(44),NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"]}];
    self.viewOldTF.attributedPlaceholder = oldPlaceHolder;
    
    [self.viewNewTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.viewNewPW.mas_left).offset(kWidthOfPt(140));
        make.right.mas_equalTo(self.viewNewPW.mas_right).offset( - kWidthOfPt(80));
        make.centerY.mas_equalTo(self.viewNewPW.mas_centerY);
        make.height.mas_equalTo(self.viewNewPW.mas_height);
    }];
    //
    NSAttributedString *newPlaceHolder = [[NSAttributedString alloc]initWithString:@"请输入新密码" attributes:@{NSFontAttributeName :systemFontOfPX(44),NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"]}];
    self.viewNewTF.attributedPlaceholder = newPlaceHolder;
    [self.viewSureTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.viewSNPW.mas_left).offset(kWidthOfPt(140));
        make.right.mas_equalTo(self.viewSNPW.mas_right).offset( - kWidthOfPt(80));
        make.centerY.mas_equalTo(self.viewSNPW.mas_centerY);
        make.height.mas_equalTo(self.viewSNPW.mas_height);
    }];
    NSAttributedString *surePlaceHolder = [[NSAttributedString alloc]initWithString:@"请确认新密码" attributes:@{NSFontAttributeName :systemFontOfPX(44),NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"]}];
    self.viewSureTF.attributedPlaceholder = surePlaceHolder;

}

- (IBAction)sublimit {
//    if (self.viewUserTF.text.length <= 0) {
//        [Tool errorWithMsg:@"请输入电话号码"];
//        return;
//    }
//    BOOL isPhoneNo = [Tool valiMobile:self.viewUserTF.text];
//    if (!isPhoneNo) {
//        [Tool errorWithMsg:@"请确认正确的电话号码"];
//        return;
//    }
    if (self.viewOldTF.text.length <= 0) {
        [Tool errorWithMsg:@"请输入旧密码"];
        return;
    }
    if (self.viewNewTF.text.length <= 0) {
        [Tool errorWithMsg:@"请输入新密码"];
        return;
    }
    
    if ([self.viewNewTF.text isEqualToString:self.viewOldTF.text]) {
        [Tool errorWithMsg:@"新密码与旧密码不能一致"];
        return;
    }
    if (self.viewSureTF.text.length <= 0) {
        [Tool errorWithMsg:@"请确认新密码"];
        return;
    }
    if (![self.viewNewTF.text isEqualToString:self.viewSureTF.text]) {
        [Tool errorWithMsg:@"两次密码输入不一致，请重新输入"];
        return;
    }
    if (self.viewNewTF.text.length < 6) {
        [Tool errorWithMsg:@"密码长度必须大于六位"];
        return;
    }
    
    
    [self sublimitInfo];
    
}
- (IBAction)isShowPW:(UIButton *)sender {
    _showPW = !_showPW;
    if (_showPW) {
        [self.viewOldTF setSecureTextEntry:NO];
        [self.viewNewTF setSecureTextEntry:NO];
        [self.viewSureTF setSecureTextEntry:NO];
        [sender setImage:[UIImage imageNamed:@"PSShowYes"] forState:UIControlStateNormal];
    }else{
        [self.viewOldTF setSecureTextEntry:YES];
        [self.viewNewTF setSecureTextEntry:YES];
        [self.viewSureTF setSecureTextEntry:YES];
        [sender setImage:[UIImage imageNamed:@"PSShowNo"] forState:UIControlStateNormal];
    }
}


- (void)sublimitInfo
{
    [SVProgressHUD showWithStatus:@"正在提交修改"];
    NSDictionary *parameters = @{@"mobileNumber":[[NSUserDefaults standardUserDefaults]objectForKey:@"USER_ACOOUNT"],@"oldpassword":[Tool md5:self.viewOldTF.text],@"newpassword":[Tool md5:self.viewNewTF.text]};
    [[WTMNetworkTool sharedNetworkTool]requestWithUrlstring:@"app/user/ModifyPassword" method:@"POST" parameters:@{@"params":parameters} success:^(id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
         NSDictionary *loginMsg = [responseObject mutableCopy];
        if ([[loginMsg objectForKey:@"status"] integerValue] == 0) {
            [Tool errorWithMsg:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [Tool errorWithMsg:[NSString stringWithFormat:@"%@",[loginMsg objectForKey:@"resultMsg"]]];
        }
    } failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [Tool errorWithMsg:[NSString stringWithFormat:@"%ld",error.code]];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self.viewUserTF resignFirstResponder];
    [self.viewNewTF resignFirstResponder];
    [self.viewOldTF resignFirstResponder];
    [self.viewSureTF resignFirstResponder];

}

-(void)textFieldDidChange:(UITextField *)textfield
{
    if (textfield.text.length > 14 ) {
        textfield.text = [textfield.text substringWithRange:NSMakeRange(0, 14)];
    }

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}


- (void)dealloc
{
    [self.viewOldTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [self.viewNewTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [self.viewSureTF removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}
@end
